package com.example.babydatacollector;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.annotation.TargetApi;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import com.example.babydatacollector.EventContract.Event;

public class ReportActivity extends Activity {
	private static final String LABEL_ID = "Event Id";
	private static final String LABEL_TYPE = "Type";
	private static final String LABEL_START_TIME = "Start Time";
	private static final String LABEL_END_TIME = "End Time";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_report);
		setContent();
		// Show the Up button in the action bar.
		setupActionBar();
	}

	/**
	 * Set up the {@link android.app.ActionBar}, if the API is available.
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void setupActionBar() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.report, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	private void setContent() {
		EventDbHelper mDbHelper = new EventDbHelper(this);
		SQLiteDatabase db = mDbHelper.getReadableDatabase();

		int eventCount = queryEventCount(db);
		List<String> events = queryEvents(db);

		String text = "Event Count: " + eventCount + "\n\n";
		for (String event : events) {
			text += event + "\n";
		}

		TextView textView = (TextView) findViewById(R.id.report);
		textView.setText(text);
	}

	int queryEventCount(SQLiteDatabase db) {
		Cursor cursor = db.query(Event.TABLE_NAME, // The table to query
				null, // The columns to return
				null, // The columns for the WHERE clause
				null, // The values for the WHERE clause
				null, // don't group the rows
				null, // don't filter by row groups
				null // The sort order
				);

		int eventCount = cursor.getCount();

		return eventCount;
	}

	List<String> queryEvents(SQLiteDatabase db) {
		List<String> events = new ArrayList<String>();

		// Define a projection that specifies which columns from the database
		// you will actually use after this query.
		String[] projection = { Event._ID, Event.COLUMN_NAME_TYPE,
				Event.COLUMN_NAME_START_TIME, Event.COLUMN_NAME_END_TIME };

		// How you want the results sorted in the resulting Cursor
		String sortOrder = Event.COLUMN_NAME_START_TIME + " DESC";

		Cursor cursor = db.query(Event.TABLE_NAME, // The table to query
				projection, // The columns to return
				null, // The columns for the WHERE clause
				null, // The values for the WHERE clause
				null, // don't group the rows
				null, // don't filter by row groups
				sortOrder // The sort order
				);

		if (cursor.moveToFirst()) {
			do {
				long id = cursor.getLong(cursor
						.getColumnIndexOrThrow(Event._ID));
				String type = cursor.getString(cursor
						.getColumnIndexOrThrow(Event.COLUMN_NAME_TYPE));
				Date startTime = new Date(cursor.getLong(cursor
						.getColumnIndexOrThrow(Event.COLUMN_NAME_START_TIME)));
				int endTimeColumnIndex = cursor
						.getColumnIndexOrThrow(Event.COLUMN_NAME_END_TIME);
				Date endTime;
				if (cursor.isNull(endTimeColumnIndex)) {
					endTime = null;
				} else {
					endTime = new Date(cursor.getLong(endTimeColumnIndex));
				}

				String event = LABEL_ID + " = " + id + "\n" + LABEL_TYPE
						+ " = " + type + "\n" + LABEL_START_TIME + " = "
						+ startTime.toString() + "\n" + LABEL_END_TIME + " = "
						+ (endTime == null ? "<null>" : endTime.toString())
						+ "\n";
				events.add(event);
			} while (cursor.moveToNext());
		}

		return events;
	}

}
